package com.library.dao;

import com.library.config.DBConnection;
import com.library.model.Reader;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReaderDAO {
    public List<Reader> getAllReaders() {
        List<Reader> readers = new ArrayList<>();
        String sql = "SELECT * FROM reader";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                readers.add(mapToReader(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return readers;
    }

    public boolean insert(Reader reader) {
        String sql = "INSERT INTO reader (reader_number, name, department, gender, telephone) VALUES (?,?,?,?,?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, reader.getReaderNumber());
            pstmt.setString(2, reader.getName());
            pstmt.setString(3, reader.getDepartment());
            pstmt.setString(4, reader.getGender());
            pstmt.setString(5, reader.getTelephone());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean update(Reader reader) {
        String sql = "UPDATE reader SET name =?, department =?, gender =?, telephone =? WHERE reader_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, reader.getName());
            pstmt.setString(2, reader.getDepartment());
            pstmt.setString(3, reader.getGender());
            pstmt.setString(4, reader.getTelephone());
            pstmt.setString(5, reader.getReaderNumber());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean delete(String readerNumber) {
        String sql = "DELETE FROM reader WHERE reader_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Reader getReaderByNumber(String readerNumber) {
        String sql = "SELECT * FROM reader WHERE reader_number =?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, readerNumber);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return mapToReader(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Reader mapToReader(ResultSet rs) throws SQLException {
        Reader reader = new Reader();
        reader.setReaderNumber(rs.getString("reader_number"));
        reader.setName(rs.getString("name"));
        reader.setDepartment(rs.getString("department"));
        reader.setGender(rs.getString("gender"));
        reader.setTelephone(rs.getString("telephone"));
        return reader;
    }
}